package org.example;

import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Objects;

public class Tools {

    public static String getResourcePath() {
        return getResourcePath("/", true);
    }

    public static String getResourcePath(String name) {
        return getResourcePath(name, true);
    }

    public static String getResourcePath(String name, boolean urlDecode) {
        var path = Objects.requireNonNull(Tools.class.getResource(name)).toString();
        try {
            URL url = new URL(path);
            path = url.getFile();
        } catch (MalformedURLException e) {
            return "";
        }

        if (urlDecode) {
            try {
                return java.net.URLDecoder.decode(path, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                return "";
            }
        } else {
            return path;
        }
    }

}



